package com.monkey4cloud.study.webfluxdemo001.service.impl;

import com.monkey4cloud.study.webfluxdemo001.dao.EmployeeDao;
import com.monkey4cloud.study.webfluxdemo001.model.Employee;
import com.monkey4cloud.study.webfluxdemo001.service.EmployeeService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

@Service
@RequiredArgsConstructor
public class EmployeeServiceImpl implements EmployeeService {

    private final EmployeeDao employeeDao;

    @Override
    public Flux<Employee> findAll() {
        return employeeDao.findAll();
    }

    @Override
    public Mono<Employee> findById(long id) {
        return employeeDao.findById(id);
    }

    @Override
    public Flux<Employee> findByName(String name) {
        return employeeDao.findByName(name);
    }

    @Override
    public Mono<Employee> save(Employee employee) {
        return employeeDao.save(employee);
    }

    @Override
    public Mono<Void> update(Employee employee) {
        return findById(employee.getId())
                .map(em -> employee.withId(em.getId()))
                .flatMap(employeeDao::save)
                .then();
    }

    @Override
    public Mono<Void> delete(Employee employee) {
        return employeeDao.delete(employee);
    }

    @Override
    public Mono<Void> deleteById(long id) {
        return employeeDao.deleteById(id);
    }
}
